/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.core.action.retrievedefault;

import com.inspur.edp.bff.api.dataprocessor.IDefaultValueConvertor;
import com.inspur.edp.bff.api.exception.BffFrameworkException;
import com.inspur.edp.bff.api.exception.ErrorCodes;
import com.inspur.edp.bff.api.manager.assembler.IAssemblerManager;
import com.inspur.edp.bff.api.manager.context.DataMapperContext;
import com.inspur.edp.bff.api.manager.context.RetrieveDefaultContext;
import com.inspur.edp.bff.entity.RetrieveDefaultParam;
import com.inspur.edp.bff.entity.defaultvalue.VoDefaultValue;
import com.inspur.edp.bff.spi.AbstractFSAction;
import com.inspur.edp.cef.entity.entity.IEntityData;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 批量新增主表数据
 */
public class RetrieveDefaultBatchAction extends AbstractFSAction<ArrayList<IEntityData>> {

    private RetrieveDefaultParam param;

    public RetrieveDefaultBatchAction(RetrieveDefaultParam param) {
        this.param = param;
    }

    @Override
    protected void execute() {
        ArrayList<IEntityData> datas = new ArrayList<>();
        if (param.getVoDefaultValues() == null || param.getVoDefaultValues().size() == 0)
            throw new BffFrameworkException(ErrorCodes.BFF_FRAMEWORK_1018);
        IAssemblerManager assemblerManager = getContext().getAssemblerManager();
        if (assemblerManager == null) {
            doDefaultRetrieveDefault();
            return;
        }
        for (VoDefaultValue defaultValue : param.getVoDefaultValues()) {
            RetrieveDefaultContext createContext = new RetrieveDefaultContext();
            createContext.setVoDefaultValue(defaultValue);
            //todo wangmj
//            createContext.setDataId(dataId);
            //① BeforeRetrieve
            assemblerManager.getRetrieveDefaultAssembler().doBeforeRetrieveDefault(getContext(), createContext);

            //② DoRetrieve
            assemblerManager.getRetrieveDefaultAssembler().doRetrieveDefault(getContext(), createContext);
            //③ DataMapping
            DataMapperContext mapperContext = new DataMapperContext();
            mapperContext.setSourceData(createContext.getRetrieveDefaultData());
            mapperContext.setIsNew(true);
            mapperContext.setVoDefaultValue(defaultValue);
            ArrayList<IEntityData> vmDatas = assemblerManager.getDataMapperAssembler().
                    doDataMapping2Vo(getContext(), mapperContext);
            createContext.setRetrieveDefaultResult(vmDatas.get(0));
            //④ AfterRetrieve
            assemblerManager.getRetrieveDefaultAssembler().doAfterRetrieveDefault(getContext(), createContext);
            if (vmDatas == null || vmDatas.size() == 0) {
                throw new BffFrameworkException(ErrorCodes.BFF_FRAMEWORK_1017);
            }
            datas.add(vmDatas.get(0));
        }


        setResult(datas);
    }

    private void doDefaultRetrieveDefault() {
        ArrayList<IEntityData> datas = new ArrayList<>();
        for (VoDefaultValue defaultValue : param.getVoDefaultValues()) {
            IEntityData beData;
            IDefaultValueConvertor convertor = getContext().getDefaultValueConvertor();
            if (defaultValue != null && convertor != null) {

                HashMap<String, Object> beDefault = convertor.convertFromVo(defaultValue);
                beData = getLcp().retrieveDefault(beDefault);
            } else {
                beData = getLcp().retrieveDefault();
            }

            IEntityData vmData = getContext().createData();
            getContext().getDataConvertor().convertFromBEData(beData, vmData);
            datas.add(vmData);
        }

        setResult(datas);
    }

}
